//
//  YFHomeRootRecomandViewController.m
//  YFListenStock
//
//  Created by funplus on 2020/12/13.
//  Copyright © 2020 ChinaRainbow-iOS. All rights reserved.
//

#import "YFHomeRootRecomandViewController.h"
#import "XLSlideSwitch.h"
#import "YFCourseViewModel.h"
#import "YFHomeRecommandViewController.h"
#import "YFLoginRegistViewModel.h"
#import "YFHomeBannerView.h"
#import "YFHomeAllViewController.h"

@interface YFHomeRootRecomandViewController ()<XLSlideSwitchDelegate>
{
    XLSlideSwitch *_slideSwitch;
}
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) NSMutableArray *bannerArray;
@property (nonatomic, strong) YFHomeBannerView *bannerView;
@property (nonatomic, assign) CGFloat offset;

@end

@implementation YFHomeRootRecomandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray arrayWithCapacity:10];
    [self initFirstView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:kLoginStatusChangeNotification object:nil];
    self.dataArray = [@[@"推荐",@"60秒语音",@"名家专栏",@"要闻解读",@"精品课程"] mutableCopy];
    [self setUpSlide];
    self.index = 0;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kShieldingUserNoti:) name:kShieldingUserNoti object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:kLoginStatusChangeNotification object:nil];
}


-(void)clickLeft
{
    self.tabBarController.selectedIndex = 3;
}


-(void)setUpSlide{
    //@"推荐",@"60秒语音",@"名家专栏",@"要闻解读",@"精品课程",@"开户"
    NSMutableArray *viewControllers = [NSMutableArray new];
    YFHomeRecommandViewController *recommandVC = [YFHomeRecommandViewController new];
    recommandVC.title = @"精选";
    recommandVC.is_better = @"1";
    recommandVC.yfScrollOffset = ^(CGFloat offset) {
        NSLog(@"%lf",offset);
        [self updateUI:offset];
    };
    recommandVC.yfMoreClick = ^{
        [self->_slideSwitch updateUIWithSelectedIndex:1 byButton:YES];
    };
    [self addChildViewController:recommandVC];
    [viewControllers addObject:recommandVC];
    [recommandVC switch_requestData];
    
    YFHomeAllViewController *allVC = [YFHomeAllViewController new];
    allVC.title = @"全部";
    [self addChildViewController:allVC];
    [viewControllers addObject:allVC];
    allVC.yfScrollOffset = ^(CGFloat offset) {
        NSLog(@"%lf",offset);
        [self updateUI:offset];
    };
    _slideSwitch = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, (100.0*(WINDOW_WIDTH)/(345.0))+28, WINDOW_WIDTH, WINDOW_HEIGHT-[YFUITool ajustTopEdgeOnIphoneX:64])];
    _slideSwitch.topHeight = 0;
    _slideSwitch.delegate = self;
    _slideSwitch.hideShadow = NO;
    _slideSwitch.topScrollView.backgroundColor = [UIColor whiteColor];
    _slideSwitch.btnSelectedColor = [UIColor zgFF4F54RedColor];
    _slideSwitch.btnNormalColor = HexColor(0x9D9D9D);
    _slideSwitch.viewControllers = viewControllers;
    [self.view addSubview:_slideSwitch];
    if (self.dataArray.count < 4) {
        _slideSwitch.adjustBtnSize2Screen = YES;
        //        _slideSwitch.adjustBtnSize2ScreenWidth = 80+[UIFont zg_fontScalSize]*4;
    }
    
    [_slideSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo((100.0*(WINDOW_WIDTH)/(345.0))+28);
    }];
    
    self.bannerView = [[YFHomeBannerView alloc]init];
    [self.view addSubview:self.bannerView];
    self.bannerView.backgroundColor = [UIColor clearColor];
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.equalTo(_slideSwitch.mas_top);
        make.height.mas_equalTo((100.0*(WINDOW_WIDTH)/(345.0))+28);
    }];
    [self getBanners];
}

-(void)getBanners{
    NSString *urlString;
    urlString = kMakeupUrl(BASEURL, @"banners");
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param safeSetObj:@"0" forKey:@"type"];
    BOOL isCache = NO;
    if (self.bannerArray.count == 0) {
        isCache = YES;
    }
    [YFBaseNetWork yf_getNetworkWithParams:param urlHost:urlString isUseCache:isCache complete:^(NSDictionary *error, NSDictionary *result) {
        if (result) {
            [self.bannerArray removeAllObjects];
            if ([result isKindOfClass:[NSArray class]]) {
                NSArray *data = (NSArray *)result;
                for (int i = 0; i < data.count ;i++ ) {
                    NSDictionary *dict = [data safeObjectWithIndex:i];
                    YFBannerModel *banner = [YFBannerModel yy_modelWithDictionary:dict];
                    [self.bannerArray addObject:banner];
                }
                self.bannerView.dataArray = [self.bannerArray copy];
                [self updateUI:self.offset];
            }
        }
    }];
}

- (void)updateUI:(CGFloat)offset{
    if (offset < 0) {
        offset = 0;
    }
    self.offset = offset;
    CGFloat top = (100.0*(WINDOW_WIDTH)/(345.0))+28;
    if (self.bannerArray.count == 0) {
        return;
    }
    top -= offset;
    if (top <= -self.view.frame.size.height) {
        top = -self.view.frame.size.height;
    }
    if (top < -((100.0*(WINDOW_WIDTH)/(345.0))+68)) {
        return;
    }
    [_slideSwitch mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(top);
    }];
}
-(void)slideSwitchDidselectTab:(NSUInteger)index{
    self.index = index;
    YFBaseViewController *VC = [_slideSwitch.viewControllers safeObjectWithIndex:index];
    [VC switch_requestData];
    [self updateUI:0];
    [self getBanners];
}
-(void)kShieldingUserNoti:(NSNotification *)notification
{
    if (self.index < _slideSwitch.viewControllers.count) {
        YFBaseViewController *VC = [_slideSwitch.viewControllers safeObjectWithIndex:self.index];
        if ([VC respondsToSelector:@selector(notification)]) {
            [VC kShieldingUserNoti:notification];
        }
    }
}
-(void)requestData
{
    if (self.index < _slideSwitch.viewControllers.count) {
        YFBaseViewController *VC = [_slideSwitch.viewControllers safeObjectWithIndex:self.index];
        [VC switch_requestData];
    }
}


-(void)initFirstView{
    self.fd_prefersNavigationBarHidden = YES;
    // 主视图colleectionView
    [self requestData];
    
}
-(void)toTopClick:(UIButton *)sender{
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark lazy load
-(NSMutableArray *)bannerArray {
    if (_bannerArray == nil) {
        _bannerArray = [NSMutableArray array];
    }
    return _bannerArray;
}
@end
